<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"])){	
	if($_REQUEST["reqtype"] == "save"){
		$errMsg .= puVarMod();
	}elseif(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "delete"){
		$errMsg .= puVarDel();
	}
	$process_unit = sxREQUEST("process_unit");
	$alias = sxREQUEST("alias");
	$variable = sxREQUEST("variable");
	$description = sxREQUEST("description");
	$read_only = (isset($_REQUEST["read_only"]))?"Y":"N";
	$note = sxREQUEST("note");
}else{
	$process_unit = sxREQ("pu");
	$alias = sxREQ("alias");
	$variable = sxREQ("var");
	$description =  "";
	$read_only = "";
	$note =  "";
}
if($process_unit != ""){
	if($alias == ""){
		$errMsg .= _t("L'alias non &egrave; definito<br>");
	}
	if($variable == ""){
		$errMsg .= _t("La variabile non &egrave; definito<br>");
	}
	if($errMsg == ""){
		$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
		$variable = addslashes(sxGPC(trim($_REQUEST["var"])));
		$sSQL = "SELECT * FROM pu_variable WHERE process_unit='$process_unit' AND variable='$variable' AND alias='$alias'";
		if($objSDAO->query($sSQL,"sel",array("contOnErr"=>true))){
			if($row = $objSDAO->fetch("sel")){
				$description = $row["description"];
				$read_only = $row["read_only"];
				$note = $row["note"];
			}
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}	
}else{
	$errMsg .= _t("Il processo non &egrave; definito<br>");
}
//output HTML
$aOpz = array("tit2"=>_t("Modifica variabile"), "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script>  
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	var aElemObbl = Array("alias", "variable");
	var aDescrElemObbl = Array("alias", "variabile");	
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"puVarMod.php\" method=\"POST\" enctype=\"multipart/form-data\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"process_unit\" id=\"process_unit\" value=\"$process_unit\">");
echo("<input type=\"hidden\" name=\"variable\" id=\"variable\" value=\"$variable\">");
echo("<input type=\"hidden\" name=\"alias\" id=\"alias\" value=\"$alias\">");
//output comandi
$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati dell'variabile"),
										"text"=>_t("conferma")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Alias")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("processo")." (*)</td>" .
			"<td class=\"text01\">$process_unit</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("alias")." (*)</td>" .
			"<td class=\"text01\">$alias</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("variabile")." (*)</td>" .
			"<td class=\"text01\">$variable</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("description")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
$chk = "";
if($read_only == "Y")
	$chk = "CHECKED";
echo("<tr>" .
			"<td class=\"labl01\">"._t("sola lettura")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"checkbox\" name=\"read_only\" id=\"read_only\" $chk></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("note")."</td>" .
			"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function puVarMod(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["variable"]) || $_REQUEST["variable"] == "")
		$errMsg .= _t("L'variable &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["alias"]) || $_REQUEST["alias"] == "")
		$errMsg .= _t("L'alias &egrave; obbligatorio").".<br>";
	
	$process_unit = addslashes(sxGPC(trim($_REQUEST["process_unit"])));
	$alias = addslashes(sxGPC(trim($_REQUEST["alias"])));
	$variable = addslashes(sxGPC(trim($_REQUEST["variable"])));
	if($errMsg == ""){
		$read_only = isset($_REQUEST["read_only"])?"Y":"N";
		$sSQL = "UPDATE pu_variable SET " .
				"description='".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"read_only='$read_only', " .
				"note='".addslashes(sxGPC(trim($_REQUEST["note"])))."' " .
				"WHERE process_unit='$process_unit' " .
				"AND variable='$variable'" .
				"AND alias='$alias'";	
		if($objSDAO->query($sSQL,"upd",array("contOnErr"=>true))){
			//elimino i profili associati
			$_SESSION[USEROBJ]->reLoad();
			logRecord("500", _t("Aggiornato la variable")." '$variable' "._t("dal processo")." '$process_unit'");
			//serverRedir("puVarList.php?pu=$process_unit&alias=$alias&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(0, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
 }
